<?php 
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 22:42:45 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1240 $
 * $Author: gavinspearhead $
 * $Id: ajax_edittransfers.php 1240 2008-06-29 20:42:45Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);
$__auth = 'silent';

$pathaet = realpath(dirname(__FILE__));

require_once "$pathaet/../functions/functions.php";
require_once "$pathaet/../functions/html_includes.php";


$groupid = get_request('groupid', '');
$cmd = get_request('cmd');

switch (strtolower($cmd)) {
case 'post' : 
    verify_challenge_text($_POST['challenge']);
    $prefs = load_config($db);
    $uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);

    $subject = get_request('subject', '');
    $poster = get_request('postername', '');
    $email = get_request('posteremail', '');
    $message = get_request('message', '');
    if (!is_numeric($groupid))
        throw new exception ('Need group ID');

    $db->insert_query('post_messages', array('userid', 'groupid', 'subject', 'poster_id', 'poster_name', 'message'),
        array($userID, $groupid, $subject, $email, $poster, $message));
    $post_id = $db->get_last_id();
    $uc->post_message($post_id); 
    $uc->disconnect();


    break;

case 'show':
    $poster_email = $prefs['poster_email'];
    $poster_name = $prefs['poster_name'];
    
    $groupinfo = get_all_active_groups($db);
    foreach($groupinfo as $gr) 
        $groups[$gr['ID']] = $gr['name'];
    natsort($groups);

    $challenge = set_challenge();
    $smarty->assign('challenge',    	$challenge);
    $smarty->assign('groups',    	    $groups);
    $smarty->assign('groupid',    	    $groupid);
    $smarty->assign('poster_name',    	$poster_name);
    $smarty->assign('poster_email',    	$poster_email);

    $smarty->display('ajax_post_message.tpl');
    die;
    break;
default:	
	die_html('Invalid command!');
}


// Success:
die_html('OK');
?>
